<?php
if (!isset($_SESSION)) session_start();
	include "functions.php";
	if(!allow_gifs())
		header('Location: index.php');
	if(isset($_GET['sort_order']))
	{
		if(trim($_GET['sort_order'])=="ASC" || trim($_GET['sort_order'])=="DESC")
		$_SESSION['sort_order'] = trim($_GET['sort_order']);
	}
	if(isset($_GET['sort_by']))
	{
		if(trim($_GET['sort_by'])=="orderid" || trim($_GET['sort_by'])=="votes" || trim($_GET['sort_by'])=="views")
			if(trim($_GET['sort_by'])=="orderid")
				$_SESSION['sort_by']="date";
			else if(trim($_GET['sort_by'])=="votes")
				$_SESSION['sort_by']="likes";
			else if(trim($_GET['sort_by'])=="views")
				$_SESSION['sort_by']="views";
		else
		$_SESSION['sort_by']="date";
	}
	if(!isset($_SESSION['sort_by']))
		$_SESSION['sort_by'] = "votes";
	if(!isset($_SESSION['sort_order']))
		$_SESSION['sort_order'] = "DESC";
	if($_SESSION['sort_by']=="date")
		$sort_by="orderid";
	else if($_SESSION['sort_by']=="likes")
		$sort_by="votes";
	else if($_SESSION['sort_by']=="views")
		$sort_by="views";
	else
		$sort_by="orderid";
		$page=1;
		$limit=posts_per_page();
		$next=2;
		$prev=1;
		$data = mysql_query("SELECT id FROM media where approved=1 and type=2");
		$rows = mysql_num_rows($data);
		$last = ceil($rows/$limit);
	if($last==0)
		$last=1;
	if(isset($_GET['page']) && $_GET['page']!='' && ($_GET['page']>=1 && $_GET['page']<=$last))
	{
		$page=$_GET['page'];
		if($page>1)
		$prev=$page-1;
		else
		$prev=$page;
		if($page<$last)
		$next=$page+1;
		else
		$next=$page;
	}
	include "header.php";
?>
	<title>Hại não - <?php echo('Trang ' . $page) ?></title>
</head>
	<?php
	include "inc/header_under.php";
	?>         
    <div id="content-holder"> 
        <div class="main-filter ">
            <ul class="content-type">
					<?php if((allow_pictures()) || (allow_videos()) || (allow_gifs())) { ?>	
					<li><a class="" href="<?php echo(rootpath()) ?>"><strong>Tất cả</strong></a></li>
					<?php } if(allow_pictures()) { ?>	
						<li> <a class="" href="<?php echo(rootpath()) ?>/pictures"><strong>Ảnh vui</strong></a></li>
					<?php } if(allow_gifs()) { ?>
						<li> <a class="current" href="<?php echo(rootpath()) ?>/hai-nao"><strong>Hại não</strong></a></li>
						<?php if(rss_enable() && rss_cat_enable()) { ?>
							<li><a class="" href="<?php echo(rootpath()) ?>/rss/gifs" style="padding-left: 8px; padding-right: 11px;"><img src="<?php echo(rootpath()) ?>/images/rss_icon.png" /></a></li>
						
					<?php } } if(allow_videos()) { ?>
						<li> <a class="" href="<?php echo(rootpath()) ?>/videos"><strong>Clip</strong></a></li> 
					<?php }?>
            </ul>		
			<div id="sort">
				<label>
					<strong>Sắp xếp theo : </strong>
						<select onchange="self.location=self.location='<?php echo(rootpath()) ?>/hai-nao.php?sort_by='+this.options[this.selectedIndex].value">
							<?php if($_SESSION['sort_by']=="date") { ?>
							<option value="orderid" selected>Ngày</option>
							<option value="votes">Lượt like</option>
							<option value="views">Lượt xem</option>
							<?php } else if($_SESSION['sort_by']=="likes") { ?>
							<option value="orderid">Ngày</option>
							<option value="votes" selected>Lượt like</option>
							<option value="views">Lượt xem</option>
							<?php } else if($_SESSION['sort_by']=="views") { ?>
							<option value="orderid">Ngày</option>
							<option value="votes">Lượt like</option>
							<option value="views" selected>Lượt xem</option>
							<?php } else { ?>
							<option value="orderid" selected>Ngày</option>
							<option value="votes">Lượt like</option>
							<option value="views">Lượt xem</option>
							<? } ?>
						</select>
				</label>​
				<?php if($_SESSION['sort_order']=="ASC") { ?>
				<a href="<?php echo(rootpath()) ?>/hai-nao.php?sort_order=DESC"><img src="<?php echo(rootpath()) ?>/images/arrow_up.png" title="Tăng dần"></a>
				<?php } else if($_SESSION['sort_order']=="DESC") { ?>
				<a href="<?php echo(rootpath()) ?>/hai-nao.php?sort_order=ASC"><img src="<?php echo(rootpath()) ?>/images/arrow_down.png" title="Giảm dần"></a>
				<? } else { ?>
				<a href="<?php echo(rootpath()) ?>/hai-nao.php?sort_order=DESC"><img src="<?php echo(rootpath()) ?>/images/arrow_up.png" title="Tăng dần"></a>
				<?php } ?>
			</div>
        </div>		
        <div id="content" listPage="hot" class="container">                      
            <div id="entries-content-new" class="grid-new"> 
            	<div class="list"> 
				<?php
					$start_result = ($page-1)*$limit;
					$sql = "select permalink,title,file from media where type=2 and approved=1 order by " . $sort_by . " " . $_SESSION['sort_order'] . " limit ".$start_result.",".$limit;
					$query = mysql_query($sql);
					$i=1;
					if(mysql_num_rows($query)>0)
					{
					while($fetch = mysql_fetch_array($query))
					{
					if($i==4)
					$i=1;
				?>
					<!--<ul id="grid-col-1" class="col-<?php echo($i) ?>">
						<li style="height:200px">
							<a href="<?php echo(rootpath()) ?>/gif/<?php echo($fetch['permalink']) ?>" class="jump_stop">
								<div style="" class="thimage">
									<img style="width: auto;" src="<?php echo(rootpath()) ?>/uploads/thumbs/<?php echo($fetch['file']) ?>" alt="<?php echo($fetch['title']) ?>" title="<?php echo($fetch['title']) ?>" />                            
									
									<div id="title_m"><?php echo($fetch['title']) ?></div></a>
								</div>							
						</li>
					</ul>-->
					<ul class="">
					<li class="article-item stickem-container">                    
	                    <div class="left fl">
	                        <h2>
	                            <a rel="" title="<?php echo($fetch['title']) ?>" href="<?php echo(rootpath(). '/gif/' . $fetch['permalink']) ?>" target="_blank">
	                                <?php echo($fetch['title']) ?>
	                            </a>
	                        </h2>
	                        <a title="<?php echo($fetch['title']) ?>" href="<?php echo(rootpath(). '/gif/' . $fetch['permalink']) ?>" target="_blank">
	                            <div class="image-thumb-box formatted">
	                                <img style="width: 500px;" alt="<?php echo($fetch['title']) ?>" src="<?php echo(rootpath()) ?>/uploads/<?php echo $fetch['file'] ?>">                                
	                            </div>                            
	                        </a>
	                        <ul class="info">
	                            <li>Đăng bởi
	                                <a href="/tac-gia/lethanhhai-9274880.htm" class="author">haiyeah</a></li>
	                            <li><span class="sprite money"></span>1.219.000</li>
	                            <li class="sep"></li>
	                            <li><span class="comment sprite"></span>
	                                <span rel="http://jqk.vn/cua-thang-nao-co-mong-44488.htm" class="comment-count bound">0</span></li>
	                            <li><span class="view sprite"></span>
	                                <span class="view-count">85</span></li>
	                            <li class="sep"></li>
	                            <li><span class="date">03 giờ trước</span></li>
	                        </ul>
	                    </div>
	                    <div class="stickem right fl k_fixer" style="">
	                        <div>
	                            <a title="Thích" class="vote normal" onclick="listStream.vote(this)" href="javascript:void(0)" rel="44488">
	                                <span class="vote-icon sprite">&nbsp;</span>
	                                <span class="vote-count">2</span> </a>
	                        </div>
	                        <div class="likefb">
	                        	<div class="share-fb">
	                               <div class="fb-share-button" data-href="http://developers.facebook.com/docs/plugins/" data-type="button"></div>
                            	</div>
	                            <div class="fb-like" data-href="https://developers.facebook.com/docs/plugins/" data-layout="box_count" data-action="like" data-show-faces="true" data-share="false" ></div>
	                        </div>
	                    </div>
	                    <div class="sep-date bound" rel="20140121" id="pnlSeperateDate44488">
	                        <div class="sep-label fl">
	                          
	                        </div>
	                        <span class="sep-button sprite fr"></span>
	                    </div>
	                </li>
            	</ul>
				<?php
					$i++;
				}
				}
				else
				{
				?>
				<div class="submitted">
					<p>
						<span class="go3d">N</span>
						<span class="go3d">o</span>
						<span class="go3d">t</span>
						<span class="go3d">&nbsp&nbsp</span>
						<span class="go3d">F</span>
						<span class="go3d">o</span>
						<span class="go3d">u</span>
						<span class="go3d">n</span>
						<span class="go3d">d</span>
					</p>   
					<p class="error" style="padding-bottom: 100px;">
							Rất tiếc, chúng tôi không tìm thấy bài viết nào
					</p>
						
				</div>
				<?php } ?>

			</div>	
			<div style="clear:both"></div>	
			<div id="paging-buttons" class="paging-buttons">
				<?php 
				$path = rootpath() . '/hai-nao';
				pagination($page,$prev,$next,$last,$path);
				?>
			</div>		
			</div>	
        </div>
    </div>
	<?php include "footer.php"; ?>
